package me.boot.web.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;

/**
 * @Description:
 * @author: Sylar
 * @date: 2022年11月21日 10:57
 */
@EnableWebSocket
@Configuration
public class WebSocketConfiguration implements WebSocketConfigurer {

    @Resource
    WebSocketHandler defaultHandler;

    @Resource
    HandshakeInterceptor defaultInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(defaultHandler,"/ws")
                .addInterceptors(defaultInterceptor)
                .setAllowedOrigins("*"); // 解决跨域问题 [4]
    }
}
